using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 游戏状态管理，管理全局游戏进度和状态
/// </summary>
public class GameState : MonoBehaviour
{
    [Header("游戏进度")]
    public string currentMapNode;
    public int currentAct = 1;
    public bool inCombat = false;
    public bool gameOver = false;
    public bool victory = false;
    public bool canChallengeBoss = false;

    [Header("玩家牌组")]
    public List<Card> masterDeck = new List<Card>(); // 所有已获得卡牌
    public List<Card> currentDeck = new List<Card>(); // 当前战斗牌组

    [Header("游戏记录")]
    public int totalGoldEarned = 0;
    public int totalEnemiesDefeated = 0;
    public int totalCardsPlayed = 0;
    public float playTime = 0f;

    [Header("当前战斗")]
    public List<EnemyData> currentEnemies = new List<EnemyData>();
    public PlayerData currentPlayer;

    // 事件
    public System.Action<string> OnMapNodeChanged;
    public System.Action<int> OnActChanged;
    public System.Action<bool> OnCombatStateChanged;
    public System.Action OnGameOver;
    public System.Action OnVictory;

    private static GameState _instance;
    public static GameState Instance => _instance;

    private void Awake()
    {
        if (_instance != null && _instance != this)
        {
            Destroy(gameObject);
            return;
        }
        
        _instance = this;
        DontDestroyOnLoad(gameObject);
        
        Debug.Log("[GameState] 游戏状态管理器初始化完成");
    }

    private void Update()
    {
        if (!gameOver && !victory)
        {
            playTime += Time.deltaTime;
        }
    }

    // 地图节点管理
    public void ChangeMapNode(string nodeId)
    {
        currentMapNode = nodeId;
        OnMapNodeChanged?.Invoke(nodeId);
        Debug.Log($"移动到地图节点: {nodeId}");
    }

    // 章节管理
    public void AdvanceToNextAct()
    {
        currentAct++;
        OnActChanged?.Invoke(currentAct);
        Debug.Log($"进入第 {currentAct} 章");
    }

    // 战斗状态管理
    public void EnterCombat(List<EnemyData> enemies, PlayerData player)
    {
        inCombat = true;
        currentEnemies = enemies;
        currentPlayer = player;
        OnCombatStateChanged?.Invoke(true);
        Debug.Log("进入战斗状态");
    }

    public void ExitCombat(bool playerVictory)
    {
        inCombat = false;
        currentEnemies.Clear();
        OnCombatStateChanged?.Invoke(false);
        
        if (playerVictory)
        {
            Debug.Log("战斗胜利");
        }
        else
        {
            Debug.Log("战斗失败");
        }
    }

    // 牌组管理
    public void AddCardToMasterDeck(Card card)
    {
        if (!masterDeck.Exists(c => c.id == card.id))
        {
            masterDeck.Add(card);
            Debug.Log($"添加卡牌到总牌库: {card.name}");
        }
    }

    public void RemoveCardFromMasterDeck(string cardId)
    {
        masterDeck.RemoveAll(c => c.id == cardId);
        Debug.Log($"从总牌库移除卡牌: {cardId}");
    }

    public void InitializeCombatDeck()
    {
        currentDeck = new List<Card>();
        foreach (var card in masterDeck)
        {
            currentDeck.Add(card.Clone());
        }
        Debug.Log($"初始化战斗牌组，共 {currentDeck.Count} 张卡牌");
    }

    // 游戏结束管理
    public void GameOver()
    {
        gameOver = true;
        inCombat = false;
        OnGameOver?.Invoke();
        Debug.Log("游戏结束");
    }

    public void Victory()
    {
        victory = true;
        inCombat = false;
        OnVictory?.Invoke();
        Debug.Log("游戏胜利！");
    }

    // 数据统计
    public void RecordEnemyDefeated()
    {
        totalEnemiesDefeated++;
    }

    public void RecordCardPlayed()
    {
        totalCardsPlayed++;
    }

    // 重置游戏状态（新游戏）
    public void ResetGameState()
    {
        currentMapNode = null;
        currentAct = 1;
        inCombat = false;
        gameOver = false;
        victory = false;
        
        masterDeck.Clear();
        currentDeck.Clear();
        currentEnemies.Clear();
        
        totalGoldEarned = 0;
        totalEnemiesDefeated = 0;
        totalCardsPlayed = 0;
        playTime = 0f;
        
        Debug.Log("游戏状态已重置");
    }

    // 获取游戏统计信息
    public string GetGameStats()
    {
        return $"游戏时间: {playTime:F1}秒 | 击败敌人: {totalEnemiesDefeated} | 使用卡牌: {totalCardsPlayed}";
    }
}